//
//  ZLLinePage.m
//  Quartz2D
//
//  Created by zhaolei on 2018/4/10.
//  Copyright © 2018年 zhaolei. All rights reserved.
//

#import "ZLLinePage.h"

typedef NS_ENUM(NSInteger , ZLLineMethodType) {
    ZLLineMethodInCoreGraphics ,
    ZLLineMethodInUIKit
};

@implementation ZLLinePage


- (void)drawRect:(CGRect)rect {
    [self drawRectWithMethodType:ZLLineMethodInUIKit];
}

/**根据不同的方法划线
 *@param type 方法类型的枚举值
 */
- (void)drawRectWithMethodType:(ZLLineMethodType)type {
    switch (type) {
        case ZLLineMethodInCoreGraphics:{
            [self drawLineMethodInCoreGraphics];
            break;
        }
        default:{
            [self drawLineMethodInUIKit];
            break;
        }
    }
}

/**利用CoreGraphics框架描绘线条
 */
- (void)drawLineMethodInCoreGraphics {
    //获取上下文
    CGContextRef contextRef = UIGraphicsGetCurrentContext();
    
    //设置起点
    CGContextMoveToPoint(contextRef, 50, 50);
    //设置终点
    CGContextAddLineToPoint(contextRef, 150, 150);
    
    //设置起点
    CGContextMoveToPoint(contextRef, 150, 50);
    //设置终点
    CGContextAddLineToPoint(contextRef, 50, 150);
    
    //设置颜色
    [[UIColor redColor] setStroke];
    //设置线宽
    CGContextSetLineWidth(contextRef, 5);
    //设置连接点样式
    CGContextSetLineJoin(contextRef, kCGLineJoinRound);
    //设置顶角样式
    CGContextSetLineCap(contextRef, kCGLineCapRound);
    
    //渲染
    CGContextStrokePath(contextRef);
}

/**利用UIKit框架描绘线条
 */
- (void)drawLineMethodInUIKit {
    //实例化对象
    UIBezierPath *bezierPath = [UIBezierPath bezierPath];
    
    //设置起点
    [bezierPath moveToPoint:CGPointMake(50, 50)];
    //设置终点
    [bezierPath addLineToPoint:CGPointMake(150, 150)];
    
    //设置颜色
    [[UIColor redColor] set];
    //设置线宽
    bezierPath.lineWidth = 5;
    //设置连接点样式
    bezierPath.lineJoinStyle = kCGLineJoinRound;
    //设置顶角样式
    bezierPath.lineCapStyle = kCGLineCapRound;
    
    //渲染
    [bezierPath stroke];
}

@end
